/*
 * @Author: SuperficialL
 * @Date: 2020-07-09 21:34:40
 * @LastEditTime: 2021-01-05 14:43:11
 * @Description: 音乐
 */

import Vue from 'vue';
import appConfig from '@/config/app.config';
import playerBuilder from './player';
import { getMusicList, getMusicDetail, getMusicLrc, getMusicUrl } from "@/api/global";

export default new Vue({
  data() {
    return {
      player: null,
      state: {
        seek: 0,
        index: 0,
        targetIndex: 0,
        volume: 0.4,
        wave: false,
        ready: false,
        muted: false,
        loading: false,
        playing: false,
        progress: 0,
      },
      list: {
        fetching: false,
        data: null,
      },
      song: {
        fetching: false,
        data: null,
      },
      lrc: {
        fetching: false,
        data: null,
      },
    };
  },
  computed: {
    // 当前歌曲
    currentSong() {
      return this.player && this.list.data
        ? this.list.data.tracks[this.state.index]
        : null;
    },
    // // 当前歌曲图片
    // currentSongPicUrl() {
    //   const pictureUrl = this.currentSong && this.currentSong.album.picUrl
    //   return pictureUrl
    // },
    // // 当前歌曲歌词
    // currentSongLrcContent() {
    //   const lrc = this.lrc.data
    //   return !lrc || lrc.nolyric ? null : lrc.lrc
    // },
  },
  methods: {
    // 获取歌曲歌词
    // fetchSongLrcWhenChangeSong() {
    //   const song = this.currentSong
    //   if (song && song.id) {
    //     this.fetchSongLrc(song.id)
    //   }
    // },
    humanizeOperation(action) {
      this.state.ready && action.bind(this.player)();
    },
    // 初始化专辑列表
    initPlayer() {
      this.fetchSongList().then((_) => {
        this.buildPlayer();
      });
    },
    // 初始化播放器
    buildPlayer() {
      playerBuilder(this);
    },
    // 获取专辑列表
    fetchSongList() {
      this.list.fetching = true;
      return getMusicList(appConfig.music.id)
        .then((res) => {
          this.list.fetching = false;
          this.list.data = res.result;
        })
        .catch(() => {
          this.list.fetching = false;
          this.list.data = null;
        });
    },
    // 获取单曲详情
    fetchSongDetail(song_id) {
      this.song.fetching = true;
      return getMusicDetail(song_id)
        .then((res) => {
          this.song.fetching = false;
          this.song.data = res.result;
        })
        .catch(() => {
          this.song.fetching = false;
          this.song.data = null;
        });
    },
    // 获取歌曲歌词
    fetchSongLrc(song_id) {
      this.lrc.fetching = true;
      return getMusicLrc(song_id)
        .then((response) => {
          this.lrc.fetching = false;
          this.lrc.data = response.result;
        })
        .catch((error) => {
          this.lrc.fetching = false;
          this.lrc.data = null;
        });
    },
    // 获取歌曲地址
    fetchSongUrl(song_id) {
      return getMusicUrl(song_id);
    },
  },
});
